package com.dg11185.nearshop.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

import com.android.nearshop.R;
import com.dg11185.nearshop.view.MyAlertDialog;
import com.wlper.lib.view.loading.LoadingDialog;

/**
 * 基础模板Activity。包含顶部一个标题栏，通过getPartView方法获取标题栏左、中、右区域， 便于添加自定义的View到标题栏
 * 
 * @author wanliang527
 * 
 */
public class BaseActivity extends Activity {

    protected final static int LEFT = 1;
    protected final static int MIDDLE = 2;
    protected final static int RIGHT = 3;

    private LinearLayout mContainer;
    private LayoutInflater mInflater;

    protected Dialog loadingDialog = null;
    protected LoadingDialog mLoadingDialog;// 加载对话框
    protected BaseActivity mActivity;
    protected SharedPreferences mPreferences;
    private MyAlertDialog mDialog;// 自定义对话框

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        mActivity = this;
        super.setContentView(R.layout.base_template_activity);
        mContainer = (LinearLayout) findViewById(R.id.llayout_container);
        mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public void setContentView(int layoutResID) {
        View view = mInflater.inflate(layoutResID, null);
        this.setContentView(view);
    }

    @Override
    public void setContentView(View view) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        params.weight = 1;
        mContainer.addView(view, params);
    }

    @Override
    public void setContentView(View view, LayoutParams params) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                params);
        layoutParams.weight = 1;
        mContainer.addView(view, layoutParams);
    }

    /**
     * 获取局部视图，用于添加相应视图
     * 
     * @param loc
     *            位置
     * @return
     */
    public LinearLayout getPartView(int loc) {
        switch (loc) {
            case LEFT:
                return (LinearLayout) findViewById(R.id.llayout_left);
            case MIDDLE:
                return (LinearLayout) findViewById(R.id.llayout_middle);
            case RIGHT:
                return (LinearLayout) findViewById(R.id.llayout_right);
            default:
                return null;
        }
    }

    /**
     * 设置顶部Banner背景
     * 
     * @param res
     *            支持 【int colorId】、【int drawableId】、【Drawable drawable】
     * 
     * @return
     */
    public boolean setTopBannerBg(Object res) {
        return com.wlper.lib.uitls.ViewUtils.setViewBackground(
                findViewById(R.id.rl_top_banner), res);
    }

    /**
     * 设置整个背景
     * 
     * @param res
     *            支持 【int colorId】、【int drawableId】、【Drawable drawable】
     * @return
     */
    public boolean setBackground(Object res) {
        return com.wlper.lib.uitls.ViewUtils.setViewBackground(
                findViewById(R.id.llayout_container), res);
    }

    /**
     * 显示加载对话框
     */
    public void showLoading() {
        if (mLoadingDialog == null)
            mLoadingDialog = new LoadingDialog(mActivity);
        mLoadingDialog.show();
    }

    /**
     * 隐藏加载对话框
     */
    public void hideLoading() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing())
            mLoadingDialog.dismiss();
    }

    public MyAlertDialog getAlertDialog() {
        if (mDialog == null) {
            mDialog = new MyAlertDialog(mActivity);
        }
        return mDialog;
    }

    public MyAlertDialog showDialog(String title, String message) {
        MyAlertDialog dialog = getAlertDialog();
        dialog.setTitle(title).setMessage(message).show();
        return dialog;
    }

    public void hideDialog() {
        if (mDialog == null)
            return;
        mDialog.dismiss();
    }
}
